<?php
$challenge_ts = toDateTime(time(), 'r');
$localTime = toLocalTime();
$ChinaTime = toChinaTime();
if ($_POST && isset($_POST['safe']) && $_POST['safe'] === 'toTime') {
    date_default_timezone_set('PRC');

    $challenge_ts = $_POST['challenge_ts'];
    $challenge_ts_results = toUnixTime($challenge_ts);
    $challenge_ts_local_r = toDateTime($challenge_ts_results, 'r');
    $challenge_ts_local_c = toDateTime($challenge_ts_results, 'c');
}

function toLocalTime()
{
    return date('Y-m-d H:i:s');
}

function toChinaTime()
{
    date_default_timezone_set('PRC');
    return date('Y-m-d H:i:s');
}

function toUtcTime($t)
{
    return $t;
}

function toUnixTime($t)
{
    return strtotime($t);
}

function toDateTime($t, $config)
{
    return date("$config", $t);
}

?>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>时间转换</title>
    <style>
        #tt{width:50%;margin:0 auto;}
        #tt > div > span{display:block;}
        #tt > div > span:nth-child(odd){color:red;}
        #tt > div > span:nth-child(even){color:green;}
        form{width:50%;margin:0 auto;}
        form > div{padding:15px;}
        form > div > label{font-weight:bold;}
        input{width:100%;}
    </style>
</head>
<body>
<div id="tt">
    <div>
        <span>当前UTC时间</span>
        <span><?php echo $localTime; ?></span>
    </div>
    <div>
        <span>当前北京时间</span>
        <span><?php echo $ChinaTime; ?></span>
    </div>
</div>

<form action="" method="post">
    <div>
        <label for="challenge_ts">challenge_ts</label>
        <input type="text" name="challenge_ts" id="challenge_ts"
               value="<?php echo $challenge_ts; ?>">
    </div>
    <div>
        <label for="challenge_ts_results">challenge_ts_results</label>
        <input type="text" name="challenge_ts_results" id="challenge_ts_results"
               value="<?php echo $challenge_ts_results; ?>">
    </div>
    <div>
        <label for="challenge_ts_local_r">challenge_ts_local_r</label>
        <input type="text" name="challenge_ts_local_r" id="challenge_ts_local_r"
               value="<?php echo $challenge_ts_local_r; ?>">
    </div>
    <div>
        <label for="challenge_ts_local_c">challenge_ts_local_c</label>
        <input type="text" name="challenge_ts_local_c" id="challenge_ts_local_c"
               value="<?php echo $challenge_ts_local_c; ?>">
    </div>
    <input type="hidden" name="safe" value="toTime">
    <input type="submit" value="转换">
    <input type="reset" value="重置">
</form>

</body>
</html>
